import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Router } from '@angular/router';
import { sencondDescentArrs } from '../used-marketing-manage.pipe';
import { PermissionService } from '../../../../service/permission.service';
import { SecondDescentService } from '../services/second-descent.service';
import { CommidityLabelManageService } from '../../commidity-mamage/commidity-label-list/commidity-label-manage.service';

@Component({
  selector: 'app-second-descent-list',
  templateUrl: './second-descent-list.component.html',
  styleUrls: ['./second-descent-list.component.scss']
})
export class SecondDescentListComponent implements OnInit {
  // 路由缓存恢复时
  static updateCache: any = null;

  typeOptions = [
    {value: 1, label: '满减'},
    {value: 2, label: '满折'},
    {value: 3, label: '多件折扣'},
    {value: 4, label: '多件立减'},
    {value: 5, label: '阶梯满减'},
    {value: 6, label: '阶梯折扣'}
  ];
  statusOptions: any = sencondDescentArrs.filter((item: any) => ![6, 7].includes(item.value));
  searchOptions = [
    { type: 1, labelText: '名称', valueKey: 'name', value: '' },
    {
      type: 2,
      labelText: '状态',
      valueKey: 'status',
      value: [],
      multiple: true,
      selectOptions: this.statusOptions,
    },
    {
      type: 2,
      labelText: '类型',
      valueKey: 'type',
      value: [],
      multiple: true,
      selectOptions: [...this.typeOptions],
    }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  }

  constructor(
    private router: Router,
    private message: NzMessageService,
    public permission: PermissionService,
    private services: SecondDescentService,
    private commidityLabelManageService: CommidityLabelManageService
  ) { }

  ngOnInit(): void {
    SecondDescentListComponent.updateCache = () => {
      this.loadTable();
    }
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const res: any = await this.services.getdescentList(params);
    this.tableParams.loading = false;

    if (res.code === 0) {
      const { records, total } = res.data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.message.error(res.message);
    }
  }

  queryData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  resetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }
  
  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }

  /**
   * 页面跳转
   * @param type 0：新增 1：编辑 2：查看 3：复制
   * @param item 
   */
  jumpPage(type: number, item?: any) {
    let url = type === 2 ? '/used-store/second-descent-details' : '/used-store/second-descent-edit';
    let obj: any = {
      type
    };
    if ([1, 2, 3].includes(type)) {
      obj.id = item.id
    }
    this.router.navigate([url],{
      queryParams: obj
    })
  }

  /**
   * 停止活动 | 删除活动
   * @param item 
   * @param status 4：停止 8：删除
   */
  async stopDelActivity(item: any, status: number) {
    let params = {
      id: item.id,
      status: status
    }
    const { code, message }: any = await this.services.updateDescentItem(params);
    if (code === 0) {
      this.message.success((status === 4 ? '停止' : '删除') + '活动成功');
      this.loadTable();
    } else {
      this.message.error(message || (status === 4 ? '停止' : '删除') + '活动失败');
    }
  }

  // 生成商品标签
  async generateGoodLable(item: any) {
    let params = {
      promotionId: item.id
    }
    
    const res: any = await this.commidityLabelManageService.addEditGoodsLabelData(params, 0);
    if (res.code === 0) {
      this.message.success('成功生成商品标签');
      this.loadTable();
    } else {
      this.message.error(res.message);
    }
  }
}
